package com.cloudptt.api.product.func.crsc;

import android.content.Context;
import android.os.SystemClock;

import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.call.CallAttr;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.CrscOperator;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.BootStrapSvrInfo;

import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.Vector;

public class CrscFunc implements IFunc
{

//	public final static String Log_Key = "CrscFunc";
	private CrscOperator operator = null;
	private CrscBroadcast broadcast = null;
//	private String uuid = null;
	private boolean canUse = false;


	public CrscFunc(Context ctx, CrscOperator operator)
	{
		Log.debug(this.getClass(), "CrscFunc");
		this.broadcast = new CrscBroadcast(ctx);
		this.operator = operator;
//		this.uuid = UUID.randomUUID().toString();
		this.canUse = false;
	}
	public void setListener(CrscListener listener) {
		this.broadcast.setListener(listener);
	}

	public List<FuncNumOb> getFuncNumObList()
	{
		Log.debug(this.getClass(),"onResume getFuncNumObList");
		if(broadcast != null)
		{
			return  broadcast.getFuncNumObList();
		}
		return new Vector<FuncNumOb>();
	}
	public String getFuncUuid()
	{
		if(broadcast != null)
		{
			broadcast.getFuncNumObList();
		}
		return UUID.randomUUID().toString();
	}
	@Override
	public void onLogined(McIdInfo mcIdInfo, BootStrapSvrInfo info, boolean isLoginSuc)
	{
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onPublishResult(int publishResult)
	{
		Log.debug(this.getClass(), "onPublishResult publishResult = " + publishResult + " canUse = " + canUse);
		if(!canUse && (publishResult & CallAttr.McType_McPtt) != 0) {

			SystemClock.sleep(100);
			operator.subcribeFuncSelf();
			canUse = true;
		}
	}
	@Override
	public void onSubscribeCmsGmsResult(boolean isSuc,int mcType)
	{}
	@Override
	public void doNetworkChange(boolean isConn)
	{
		Log.debug(this.getClass(), "networkOffline isConn = " + isConn);
	}
	@Override
	public void doForceDetach()
	{
		Log.debug(this.getClass(), "doForceDetach");
//		sendFuncLogoutAll();
	}
	@Override
	public void doLogout() {
		Log.debug(this.getClass(), "doLogout");
		canUse = false;
		if(broadcast != null)
		{
			broadcast.clearFuncNumObList();
		}
//		sendFuncLogoutAll();
	}
	public void doExit()
	{
		Log.debug(this.getClass(), "doExit");
//		sendFuncLogoutAll();
		this.broadcast.clearResource();
		this.broadcast = null;
	}

	public void sendFuncLogoutAll()
	{
		List<FuncNumOb> obList = broadcast.getFuncNumObList();
		Log.debug(this.getClass(), "sendFuncLogoutAll obList = " + obList);
		String[] list = new String[obList.size()];
		int i = 0;
		for (Iterator<FuncNumOb> itor = obList.iterator();itor.hasNext();)
		{
			FuncNumOb ob = itor.next();
			list[i] = ob.getFuncNum();
			i++;
		}
		Log.debug(this.getClass(), "sendFuncLogoutAll list.length = " + list.length);
		operator.sendFuncLogoutAll(list);
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		
	}
	
}
